package thread.matrix.row;

import java.util.ArrayList;
import java.util.List;

/**
 * 每行一个线程，计算结果矩阵的值
 */
public class RowMultiplier {
    public static void multiply(double[][] matrix1,double[][] matrix2,double[][] result){
        List<Thread> threads = new ArrayList<>();
        for(int i=0;i<matrix1.length;i++){//matrix1的行数
            //每行一个线程，计算结果矩阵的值
            //创建线程
            Thread t = new Thread(new RowMultiplierTask(matrix1,matrix2,result,i));
            t.start();
            threads.add(t);
            if (threads.size()%10==0){
                waitForThread(threads);
            }
        }
    }

    private static void waitForThread(List<Thread> threads) {
        for (Thread t:threads){
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
